package ms.jasim.console;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Collection;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ms.spm.IAppContext;
import ms.spm.implement.SimpleMethodProvider;
import ms.spm.services.ActionCreator;

public class AgentStatusFrame extends JasimDockablePanel {
	private static final long serialVersionUID = 1L;
	
	private JTextArea textArea;
	private ActionCreator svcActCreator; 
	

	/**
	 * Create the frame
	 */
	public AgentStatusFrame(IAppContext ctx) {
		super(ctx.createChildContext("$agentStatusFrame"));
		setText("Agent Status");
		
		createContent();
		try {
			refresh(null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void createContent() {
		setLayout(new BorderLayout());
		
		setBounds(100, 100, 500, 375);

		textArea = new JTextArea();
		textArea.setInheritsPopupMenu(true);
		textArea.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		add(scrollPane);

		final JPopupMenu popupMenu = new JPopupMenu();
		
		popupMenu.add(new JMenuItem(svcActCreator.loadAction("Refresh", context)));
		
		addPopup(textArea, popupMenu);
	}

	@Override
	protected void setupContext() {
		super.setupContext();
		context.addService(new SimpleMethodProvider(this, context));
		
		svcActCreator = context.getService(ActionCreator.class);
		svcJasim.addListener(new JasimClientListener() {
			@Override
			public void notifyListener() {
				try {
					refresh(null);
				} catch (IOException e) {
				}
			}
		});
	}

	void refresh(ActionEvent e) throws IOException {
		Collection<JasimAgent> agents = svcJasim.getAgents();
		if (agents != null) {
			StringBuilder s = new StringBuilder();
			for(JasimAgent ag: agents) {
				s.append("AGENT " + ag.getName() + ":\n");
				for(String st: ag.getStatus())
					s.append("\t" + st + "\n");
			}
			textArea.setText(s.toString());
		}
		else
			textArea.setText("");
	}
	
	/**
	 * WindowBuilder generated method.<br>
	 * Please don't remove this method or its invocations.<br>
	 * It used by WindowBuilder to associate the {@link javax.swing.JPopupMenu}
	 * with parent.
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger())
					showMenu(e);
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger())
					showMenu(e);
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}



}
